package com.lwc.cfdns.pojo.bo.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lwc.cfdns.enums.ResultCode;
import com.lwc.cfdns.exception.NullDataOfSearchException;
import com.lwc.cfdns.pojo.bo.AbstractPageQuery;
import com.lwc.cfdns.pojo.entity.SystemAdmin;
import com.lwc.cfdns.service.impl.SystemAdminDeptServiceImpl;
import com.lwc.cfdns.service.impl.SystemAdminRoleServiceImpl;
import com.lwc.cfdns.utils.ConvertUtils;
import com.lwc.cfdns.utils.SpringContextUtils;
import com.lwc.cfdns.utils.StringUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

/**
 * 管理员查询
 *
 * @author luwc
 * @title AdminBO
 * @description
 * @time 2023/5/24 11:02:00
 * @version: 1.0
 */
@Data
public class AdminBO extends AbstractPageQuery {
    private String status;
    private String userName;
    private String phonenumber;
    private String stime;
    private String etime;
    private String deptId;
    private String roleId;
    private String notRoleId;

    /**
     * 生成query conditions
     *
     * @return LambdaQueryWrapper<T>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/4/26 20:17:17
     */
    @Override
    public LambdaQueryWrapper<SystemAdmin> toQueryWrapper() {
        LambdaQueryWrapper<SystemAdmin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 按字段查询
//        lambdaQueryWrapper.select(SystemAdmin::getUser_name);
        if (StringUtils.isNotEmpty(this.status)) {
            lambdaQueryWrapper.eq(SystemAdmin::getStatus, this.status);
        }
        if (StringUtils.isNotEmpty(this.userName)) {
            lambdaQueryWrapper.like(SystemAdmin::getUserName, this.userName);
        }
        if (StringUtils.isNotEmpty(this.phonenumber)) {
            lambdaQueryWrapper.like(SystemAdmin::getPhonenumber, this.phonenumber);
        }
        if (StringUtils.isNotEmpty(deptId)) {
            List<Object> adminIds = SpringContextUtils.getBean(SystemAdminDeptServiceImpl.class).getAdminIds(ConvertUtils.toLong(deptId));
            if (adminIds.size() > 0) {
                lambdaQueryWrapper.in(SystemAdmin::getId, adminIds);
            } else {
                // 查不到数据直接返到前端了。
                throw new NullDataOfSearchException(ResultCode.SUCCESS_NOT_DATA_FOUNT);
            }
        }
        // 根据角色查询
        if (StringUtils.isNotEmpty(roleId)) {
            List<Object> adminIds = SpringContextUtils.getBean(SystemAdminRoleServiceImpl.class).getAdminIds(ConvertUtils.toLong(roleId));
            if (adminIds.size() > 0) {
                lambdaQueryWrapper.in(SystemAdmin::getId, adminIds);
            } else {
                // 查不到数据直接返到前端了。
                throw new NullDataOfSearchException(ResultCode.SUCCESS_NOT_DATA_FOUNT);
            }
        }

        // 没在此角色下的用户查询
        if (StringUtils.isNotEmpty(notRoleId)) {
            List<Object> adminIds = SpringContextUtils.getBean(SystemAdminRoleServiceImpl.class).getAdminIds(ConvertUtils.toLong(notRoleId));
            if (adminIds.size() > 0) {
                lambdaQueryWrapper.notIn(SystemAdmin::getId, adminIds);
            }
        }

        try {
            // 时间区间(时间戳)模式
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
//            if (StringUtils.isNotEmpty(this.stime)) {
//                lambdaQueryWrapper.ge(SystemAdmin::getCreated_at, simpleDateFormat.parse(this.stime + " 00:00:00").getTime() / 1000);
//            }
//            if (StringUtils.isNotEmpty(this.etime)) {
//                lambdaQueryWrapper.le(SystemAdmin::getCreated_at, simpleDateFormat.parse(this.etime + " 23:59:59").getTime() / 1000);
//            }
            // 日期模式
            if (StringUtils.isNotEmpty(this.stime)) {
                lambdaQueryWrapper.ge(SystemAdmin::getCreatedAt, this.stime);
            }
            if (StringUtils.isNotEmpty(this.etime)) {
                lambdaQueryWrapper.le(SystemAdmin::getCreatedAt, this.etime);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        lambdaQueryWrapper.orderByDesc(SystemAdmin::getId);

        return lambdaQueryWrapper;
    }
}
